package com.china08.yxyapi.api.manage.edu.platform.statistics.weikepandect;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResWeikeKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResWeikeKnowRepository;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceSubNameCountService;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 微课统计 各科目微课
 * 
 */
@CrossOrigin
@RestController
public class WeikeCountEachSubjectController extends YxyApiBaseController {
	@Autowired
	public ResourceSubNameCountService resourceSubNameCountService;
	@Autowired
	public YxyResSubjectRepository yxyResSubjectRepository;
	@Autowired
	public YxyResWeikeKnowRepository yxyResWeikeKnowRepository;

	@ApiOperation(value = "微课统计     各科目微课")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/weikepandect/eachsubject/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req) {
		RespModel model = new RespModel();
		Integer schoolBureauLevel = getProfile(request, response).getSchoolBureauLevel();
		String districtCode = null;
		if (schoolBureauLevel == 0) {
			districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
		} else if (schoolBureauLevel == 1) {
			districtCode = getProfile(request, response).getSchoolAreaIdCity();
		} else if (schoolBureauLevel == 2) {
			districtCode = getProfile(request, response).getSchoolAreaIdProvince();
		}
		Map<String, Object> countLst = null;

		DecimalFormat df = new DecimalFormat("######0.00");

		// 名称和具体数据
		List<Object[]> countLstObj2 = new ArrayList<Object[]>();
		// 调用此方法查询资源数据总览数据

		if (req.stageName != null) {
			countLst = resourceSubNameCountService.statisticsWeikePhaseCount(req.stageName, districtCode,
					schoolBureauLevel);
			Integer zong = 0;
			for (String key1 : countLst.keySet()) {
				zong += Integer.parseInt(countLst.get(key1).toString());
			}
			Object[] objs2 = null;
			for (String key : countLst.keySet()) {
				objs2 = new Object[2];
				YxyResWeikeKnow yxyResWeikeKnow = yxyResWeikeKnowRepository.findTopByWeikeSubjectAndWeiKeStatus(key, 1);

				if (yxyResWeikeKnow != null) {

					objs2[0] = yxyResWeikeKnow.getWeikeSubjectName();
					objs2[1] = Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
					countLstObj2.add(objs2);
				}

			}

		}
		String[] subjectNameList = new String[] { "语文", "数学", "英语", "思想政治", "物理", "化学", "历史", "生物", "地理" };
		List<Object[]> subList = new ArrayList<Object[]>();
		if (countLstObj2.size() > 0) {
			Object[] obj = null;
			for (String subjectName : subjectNameList) {

				for (Object[] objects : countLstObj2) {
					if (subjectName.equals(objects[0])) {
						obj = new Object[2];
						obj[0] = objects[0];
						obj[1] = objects[1];
						subList.add(obj);
					}
				}
			}

		}
		// 将新组合的list里的学段名称值连接在一起
		String phaseName = "";
		for (Object[] objects : subList) {
			phaseName += objects[0] + ",";
		}
		if (countLstObj2.size() > subList.size()) {
			for (Object[] countLstObj : countLstObj2) {

				// 搜索字符串中是否包含数组里的元素
				if (phaseName.indexOf(countLstObj[0].toString()) == -1) {
					subList.add(countLstObj);
				}
			}
		}
		model.countLstObj2 = subList;
		return model;
	}

	protected static class RespModel extends YxyApiRespModel {
		// 名称和具体数据
		public List<Object[]> countLstObj2;
	}

	protected static class ReqModel extends YxyApiReqModel {
		// 学段名称
		public String stageName;
	}
}
